package com.ruoyi.wechat.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wechat.domain.CampFacility;
import com.ruoyi.wechat.domain.FacilityType;
import com.ruoyi.wechat.mapper.CampFacilityMapper;
import com.ruoyi.wechat.mapper.FacilityTypeMapper;
import com.ruoyi.wechat.service.IWxFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class WxFacilityServiceImpl implements IWxFacilityService {
    @Autowired
    private FacilityTypeMapper facilityTypeMapper;
    @Autowired
    private CampFacilityMapper campFacilityMapper;

    /**
     * 查询所有启用的服务设施类型及对应的服务设施
     * @return
     */
    @Override
    public List<FacilityType> facilityTypeList() {
        return facilityTypeMapper.selectFacilityTypeAndFacilityList();
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public int insertCampFacility(String campId, String[] facilityIds) {
        //先删除营地下的服务设施
        campFacilityMapper.deleteCampFacilityByCampId(campId);
        //然后插入新的服务设施
        for(String facilityId:facilityIds){
            CampFacility campFacility=new CampFacility();
            campFacility.setCfId(IdUtils.randomUUID());
            campFacility.setCampId(campId);
            campFacility.setFacilityId(facilityId);
            campFacilityMapper.insertCampFacility(campFacility);
        }

        return 1;
    }
    /**
     * 根据营地Id查询还营地下所有的服务设施列表
     * @param campId
     * @return
     */
    @Override
    public List<FacilityType> campFacilityListByCampId(String campId) {
        return facilityTypeMapper.selectFacilityTypeAndFacilityByCampId(campId);
    }

    @Override
    public int deleteCampFacilityByCampId(String campId) {
        return campFacilityMapper.deleteCampFacilityByCampId(campId);
    }
}
